package cn.kgc.shiro02.config;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * @author: mosin
 * @version: v1.0  2022/5/31
 * shiro权限框架的配置类
 */
@Configuration
public class MyShiroConfig {

    //获取一个ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        //设置登录的地址  默认login.jsp
        shiroFilterFactoryBean.setLoginUrl("/log.jsp");

        LinkedHashMap<String, String> map = new LinkedHashMap<>();

        map.put("/user/login", "anon");
        map.put("/reg.jsp", "anon");
        map.put("/user/reg", "anon");
        map.put("/user/logout", "anon");
        map.put("/index.jsp", "authc");
        map.put("/**", "authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm customerMD5Realm){

        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(customerMD5Realm);
        return  defaultWebSecurityManager;
    }

    @Bean
    public  CustomerMD5Realm customerMD5Realm(){
        CustomerMD5Realm customerMD5Realm = new CustomerMD5Realm();
        HashedCredentialsMatcher md5 = new HashedCredentialsMatcher("MD5");
        md5.setHashIterations(1024);
        customerMD5Realm.setCredentialsMatcher(md5);
        return customerMD5Realm;
    }



}
